package com.example.modellol.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.modellol.R;
import com.example.modellol.bean.News;

import java.util.ArrayList;

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.myViewHodler> {
    private Context context;
    private ArrayList<News> goodsEntityList;

    //创建构造函数
    public MyAdapter2(Context context, ArrayList<News> goodsEntityList) {
        //将传递过来的数据，赋值给本地变量
        this.context = context;//上下文
        this.goodsEntityList = goodsEntityList;//实体类数据ArrayList
    }

    @Override
    public myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建自定义布局
        View itemView = View.inflate(context, R.layout.fragment1, null);
        return new myViewHodler(itemView);
    }

    @Override
    public void onBindViewHolder(myViewHodler holder, int position) {
        //根据点击位置绑定数据
        News data = goodsEntityList.get(position);
//        holder.mItemGoodsImg;
        holder.text_one.setText(data.text_one);
        holder.text_two.setText(data.text_two);
        holder.text_three.setText(data.text_three);
        holder.imgPath_one.setImageResource(data.imgPath_one);
        holder.imgPath_two.setImageResource(data.imgPath_two);

    }

    @Override
    public int getItemCount() {
        return goodsEntityList.size();
    }

    //自定义viewhodler
    class myViewHodler extends RecyclerView.ViewHolder {
        private ImageView imgPath_one;
        private ImageView imgPath_two;
        private TextView text_one;
        private TextView text_two;
        private TextView text_three;

        public myViewHodler(View itemView) {
            super(itemView);
            imgPath_one = (ImageView) itemView.findViewById(R.id.img_cover);
            text_one = (TextView) itemView.findViewById(R.id.tv_article_title);
            text_two = (TextView) itemView.findViewById(R.id.tv_tag);
            imgPath_two = (ImageView) itemView.findViewById(R.id.img_view_icon);
            text_three = (TextView) itemView.findViewById(R.id.tv_article_user);
            //点击事件放在adapter中使用，也可以写个接口在activity中调用
            //方法一：在adapter中设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //可以选择直接在本位置直接写业务处理
                    //Toast.makeText(context,"点击了xxx",Toast.LENGTH_SHORT).show();
                    //此处回传点击监听事件
                    if(onItemClickListener!=null){
                        onItemClickListener.OnItemClick(v, goodsEntityList.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        public void OnItemClick(View view, News data);
    }

    //需要外部访问，所以需要设置set方法，方便调用
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}